package com.sugon.controller.tool;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.annotation.DataFilter;
import com.sugon.entity.ToolsDemandPlanEntity;
import com.sugon.entity.ToolsDemandPlanItemEntity;
import com.sugon.modules.sparepart.consts.SparePartNeedsPlanProgress;
import com.sugon.modules.tools.model.vo.ToolsDemandPlanItemVO;
import com.sugon.modules.tools.service.IToolsNeedsPlanManager;
import com.sugon.service.ToolsDemandPlanItemService;
import com.sugon.service.ToolsDemandPlanService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import com.sugon.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 备件需求计划明细Controller
 *
 * @author sugon
 * @date 2019-08-02 14:31:17
 */
@Controller
@RequestMapping("toolsdemandplandetail")
public class ToolsDemandPlanItemController {

    @Autowired
    private ToolsDemandPlanService toolsDemandPlanService;
    @Autowired
    private ToolsDemandPlanItemService toolsDemandPlanItemService;
    @Autowired
    private IToolsNeedsPlanManager toolsNeedsPlanManager;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("toolsdemandplandetail:list")
    @DataFilter(userAlias = "ORGAN_USER", deptAlias = "ORGAN_ID")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<ToolsDemandPlanItemVO> sparePartRequDetailList = toolsDemandPlanItemService.queryList(query);
        int total = toolsDemandPlanItemService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(sparePartRequDetailList, total, query.getLimit(), query.getPage());
        // 设置汇总金额
        pageUtil.put("total", toolsDemandPlanItemService.countTotalMoney(MapUtil.getStr(params, "planId")));
        return R.ok().put("page", pageUtil);
    }

    /**
     * 取消在途
     */
    @RequestMapping("/cancelTransit")
    @RequiresPermissions("toolsdemandplandetail:save")
    @ResponseBody
    public R list(@RequestParam String sparePartRequDetailId) {
        // 校验需求计划是否存在
        ToolsDemandPlanItemEntity sparePartRequDetail = toolsDemandPlanItemService.queryObject(sparePartRequDetailId);
        if (sparePartRequDetail == null) {
            return R.error("需求计划条目不存在!");
        }
        // 校验是否已取消过在途数
        if (sparePartRequDetail.getIsCancelTransit() == 1) {
            return R.error("已取消,无需重复取消!");
        }
        // 取消在途
        return toolsNeedsPlanManager.cancelTransit(sparePartRequDetail) ? R.ok() : R.error();
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("toolsdemandplandetail:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ToolsDemandPlanItemVO toolsDemandPlanItem = toolsDemandPlanItemService.queryObject(id);
        return R.ok().put("sparePartRequDetail", toolsDemandPlanItem);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("toolsdemandplandetail:save")
    @ResponseBody
    public R save(@RequestBody ToolsDemandPlanItemEntity toolsDemandPlanItem) {
        String planId = toolsDemandPlanItem.getPlanId();
        if (StrUtil.isEmpty(planId)) {
            return R.error("工装需求计划ID必填！");
        }
        // 1、校验备件需求计划是否存在
        ToolsDemandPlanEntity sparePartRequ = toolsDemandPlanService.queryObject(planId);
        if (sparePartRequ == null) {
            return R.error("备件需求计划不存在，请确认！");
        }
        // 2、只有处于编制中的需求才可以新增明细
        int progress = sparePartRequ.getProgress();
        if (progress != SparePartNeedsPlanProgress.NEW && progress != SparePartNeedsPlanProgress.NOT_PASSED) {
            return R.error("只有处于编制中的需求才可以新增明细！");
        }
        // 3、只有自己车间的人才可以添加
        if (!sparePartRequ.getOrganId().equals(ShiroUtils.getWorkshop())) {
            return R.error("只可以编制自己车间的需求计划明细！");
        }
        // 4、校验是否已添加过
        if (toolsDemandPlanItemService.isExists(planId, toolsDemandPlanItem.getMaterialId())) {
            return R.error("当前备件需求已存在，请勿重复添加！");
        }
        toolsDemandPlanItem.setOrganUser(ShiroUtils.getUserId());
        return toolsDemandPlanItemService.save(toolsDemandPlanItem) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("toolsdemandplandetail:update")
    @ResponseBody
    public R update(@RequestBody ToolsDemandPlanItemEntity toolsDemandPlanItem) {
        String planId = toolsDemandPlanItem.getPlanId();
        if (StrUtil.isEmpty(planId)) {
            return R.error("工装需求计划ID必填！");
        }
        // 1、校验备件需求计划是否存在
        ToolsDemandPlanEntity sparePartRequ = toolsDemandPlanService.queryObject(planId);
        if (sparePartRequ == null) {
            return R.error("备件需求计划不存在，请确认！");
        }
        // 2、只有处于编制中的需求才可以新增明细
        int progress = sparePartRequ.getProgress();
        if (progress != SparePartNeedsPlanProgress.NEW && progress != SparePartNeedsPlanProgress.NOT_PASSED) {
            return R.error("只有处于编制中的需求才可以修改明细！");
        }
        // 3、只有自己车间的人才可以添加
        if (!sparePartRequ.getOrganId().equals(ShiroUtils.getWorkshop())) {
            return R.error("只可以编制自己车间的需求明细！");
        }
        // 4、只可以修改自己编制的需求明细
        if (!toolsDemandPlanItem.getOrganUser().equals(ShiroUtils.getUserId())) {
            return R.error("只可以修改自己编制的需求明细！");
        }
        return toolsDemandPlanItemService.update(toolsDemandPlanItem) > 0 ? R.ok() : R.error();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("toolsdemandplandetail:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        toolsDemandPlanItemService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<ToolsDemandPlanItemVO> list = toolsDemandPlanItemService.queryList(params);
        return R.ok().put("list", list);
    }
}
